package com.tongguan.paypos.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.tongguan.paypos.R;

/**
 * Created by Joylive on 2016/10/12 .
 */

public class CustomAlertDialog extends Dialog {

    public CustomAlertDialog(Context context) {
        super(context);
    }

    public CustomAlertDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context mContext;
        private String mTitle;
        private String mMessage;
        private String mPositiveButtonText;
        private String mNegativeButtonText;
        private View mContentView;

        private OnClickListener mPositiveButtonClickListener,
                mNegativeButtonClickListener;

        public Builder(Context context) {
            mContext = context;
        }

        /**
         * 设置对话框的标题
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        /**
         * 通过资源文件设置对话框的标题
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            mTitle = (String) mContext.getText(title);
            return this;
        }

        /**
         * 设置对话框的文本
         *
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            mMessage = message;
            return this;
        }

        /**
         * 通过资源文件设置对话框的文本
         *
         * @param message
         * @return
         */
        public Builder setMessage(int message) {
            mMessage = (String) mContext.getText(message);
            return this;
        }

        /**
         * Set a custom content view for the Dialog. If a message is set, the
         * contentView is not added to the Dialog...
         *
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            mContentView = v;
            return this;
        }

        /**
         * 设置确定按钮的标题和监听器
         *
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
            mPositiveButtonText = (String) mContext.getText(positiveButtonText);
            mPositiveButtonClickListener = listener;
            return this;
        }

        /**
         * 设置确定按钮的标题和监听器
         *
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            mPositiveButtonText = positiveButtonText;
            mPositiveButtonClickListener = listener;
            return this;
        }

        /**
         * 设置取消按钮的标题和监听器
         *
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
            mNegativeButtonText = (String) mContext.getText(negativeButtonText);
            mNegativeButtonClickListener = listener;
            return this;
        }

        /**
         * 设置取消按钮的标题和监听器
         *
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            mNegativeButtonText = negativeButtonText;
            mNegativeButtonClickListener = listener;
            return this;
        }

        /**设置延时确认
         * 确认按钮和取消按钮不能为空 ，防止取消不了定时器
         * @param isTimer
         * @return
         */
        public Builder setTimer(boolean isTimer) {
            if (isTimer){
               mHandler.postDelayed(mRunnable,10000);
            }
            return this;
        }

        private Handler mHandler = new Handler();
        private Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                mSureTextView.callOnClick();
            }
        };

        public Builder show() {
            final CustomAlertDialog dialog = create();
            dialog.show();
            return this;
        }

        private TextView mTitleTextView;
        private TextView mMessageTextView;
        private TextView mSureTextView;
        private TextView mCancelTextView;
        private ImageView mImageView;

        /**
         * 创建定制的对话框
         */
        public CustomAlertDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 创建指定样式的对话框
            final CustomAlertDialog dialog = new CustomAlertDialog(mContext, R.style.CustomDialog);
            // 获取对话框的布局
            View layout = inflater.inflate(R.layout.custom_dialog_view, null);
            // 为对话框添加布局和设置大小
            dialog.addContentView(layout, new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT));
            mTitleTextView = (TextView) layout.findViewById(R.id.txt_title);
            mMessageTextView = (TextView) layout.findViewById(R.id.txt_content);
            mSureTextView = (TextView) layout.findViewById(R.id.txt_sure);
            mCancelTextView = (TextView) layout.findViewById(R.id.txt_cancel);
            mImageView = (ImageView) layout.findViewById(R.id.iv_line);

            // 设置对话框的标题
            mTitleTextView.setText(mTitle);
            // 显示文本内容
            mMessageTextView.setText(mMessage);

            // 设置确定按钮
            if (mPositiveButtonText != null) {
                // 如果有文本设置按钮的文本
                mSureTextView.setText(mPositiveButtonText);

                if (mPositiveButtonClickListener != null) {
                    // 设置按钮的监听器
                    mSureTextView.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            mPositiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                            dialog.dismiss();
                            mHandler.removeCallbacks(mRunnable);
                        }
                    });
                }else {
                  mSureTextView.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          dialog.dismiss();
                      }
                  });
                }
            } else {
                // 如果没有文本就设置按钮不可见
                mSureTextView.setVisibility(View.GONE);
                mImageView.setVisibility(View.GONE);
            }
            // 设置取消按钮
            if (mNegativeButtonText != null) {
                mCancelTextView.setText(mNegativeButtonText);
                if (mNegativeButtonClickListener != null) {
                    //设置按钮监听器
                    mCancelTextView.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            mNegativeButtonClickListener.onClick(dialog,
                                    DialogInterface.BUTTON_NEGATIVE);
                            dialog.dismiss();
                            mHandler.removeCallbacks(mRunnable);
                        }
                    });
                }else{
                    mCancelTextView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                }
            } else {
                // 如果没有文本就设置按钮不可见
                mCancelTextView.setVisibility(View.GONE);
                mImageView.setVisibility(View.GONE);
            }
            //默认不能点击对话框其他区域退出 和按返回键退出
            dialog.setCancelable(false);
            dialog.setContentView(layout);
            return dialog;
        }

    }
}
